数学函数和操作符号
最近更新时间: 2024-06-12 15:06:00
数学操作符
操作符 |
描述 |
示例 |
结果 |
+ |
加 |
3 + 4 |
7 |
- |
减 |
10 - 9 |
1 |
* |
乘 |
3 * 5 |
15 |
/ |
除(整数除法截断结果) |
10 / 4 |
2 |
% |
模(取余) |
6 % 4 |
2 |
^ |
指数 |
2 ^ 3 |
8 |
|/ |
平方根 |
|/ 16 |
4 |
||/ |
立方根 |
||/ 8 |
2 |
! |
阶乘 |
5 ! |
120 |
!! |
阶乘(前缀操作符) |
!! 5 |
120 |
@ |
绝对值 |
@ (-5) |
5 |
& |
按位与 |
2 & 3 |
2 |
| |
按位或 |
2 | 3 |
3 |
# |
按位异或 |
2 # 3 |
1 |
~ |
按位求反 |
~ 2 |
-3 |
<< |
按位左移 |
2 << 3 |
16 |
>> |
按位右移 |
8 >> 2 |
2 |
其中,按位操作操作符只能用于整数数据类型,而其它的操作符可以用于全部数字数据类型。按位操作的操作符还可以用于位串类型 bit 和 bit varying。
数学函数
函数 |
描述 |
示例 |
结果 |
abs(x) |
绝对值 |
abs(-17.4) |
17.4 |
cbrt(dp) |
立方根 |
cbrt(27.0) |
3 |
ceil(dp or numeric) |
不小于参数的最小整数 |
ceil(-42.8) |
-42 |
ceiling(dp or numeric) |
不小于参数的最小整数(ceil 的别名) |
ceiling(-95.3) |
-95 |
degrees(dp) |
把弧度转为角度 |
degrees(0.5) |
28.6478897565412 |
div(y numeric, x numeric) |
y/x 的整数商 |
div(9,4) |
2 |
exp(dp or numeric) |
指数 |
exp(1.0) |
2.7182818284590452 |
floor(dp or numeric) |
不大于参数的最大整数 |
floor(-42.8) |
-43 |
ln(dp or numeric) |
自然对数 |
ln(2.0) |
0.6931471805599453 |
log(dp or numeric) |
以10为底的对数 |
log(100.0) |
2.0000000000000000 |
log(b numeric, x numeric) |
以b为底的对数 |
log(2.0, 64.0) |
6.0000000000000000 |
mod(y, x) |
y/x 的余数 |
mod(9,4) |
1 |
pi() |
“π” 常数 |
pi() |
3.14159265358979 |
power(a dp, b dp) |
求 a 的 b 次幂 |
power(9.0, 3.0) |
729.0000000000000000 |
power(a numeric, b numeric) |
求 a 的 b 次幂 |
power(9.0, 3.0) |
729.0000000000000000 |
radians(dp) |
把角度转为弧度 |
radians(45.0) |
0.785398163397448 |
round(dp or numeric) |
圆整为最接近的整数 |
round(42.4) |
42 |
round(v numeric, s int) |
圆整为s位小数数字 |
round(42.4382, 2) |
42.44 |
scale(numeric) |
参数的精度(小数点后的位数) |
scale(8.41) |
2 |
sign(dp or numeric) |
参数的符号(-1, 0, +1) |
sign(-8.4) |
-1 |
sqrt(dp or numeric) |
平方根 |
sqrt(2.0) |
1.414213562373095 |
trunc(dp or numeric) |
截断(向零靠近) |
trunc(42.8) |
42 |
trunc(v numeric, s int) |
截断为s位小数位置的数字 |
trunc(42.4382, 2) |
42.43 |
width_bucket(operand dp, b1dp, b2 dp, count int) |
返回一个桶,这个桶是在一个有 count 个桶, 上界为 b1,下界为 b2 的柱图中 operand 将被赋予的那个桶。为外部范围输入返回0或者 count+1 |
width_bucket(5.35, 0.024, 10.06, 5) |
3 |
width_bucket(operand numeric, b1 numeric, b2 numeric, countint) |
返回一个桶,这个桶是在一个有 count 个桶,上界为 b1,下界为 b2 的柱图中 operand 将被赋予的那个桶; 为范围外的输入返回0或者 count+1 |
width_bucket(5.35, 0.024, 10.06, 5) |
3 |
width_bucket(operandanyelement, thresholdsanyarray) |
返回一个桶,它是给定数组列出桶的下限 operand 将被赋予的那个桶, 为了输入低于第一下界返回0;thresholds 数组必须被存储,首先最小值,或者获取意想不到的结果 |
width_bucket(now(), array['yesterday', 'today', 'tomorrow']::timestamptz[]) |
2 |
随机函数
函数 |
返回类型 |
描述 |
random() |
dp |
范围 0.0 <= x < 1.0 中的随机值 |
setseed(dp) |
viod |
为后续的 random() 调用设置种子(值位于-1.0和1.0之间,包括边界值) |
三角函数
所有三角函数都有类型为 double precision 的参数和返回类型。
三角函数参数表示为弧度。
反函数返回表示为弧度的值。
函数(弧度) |
函数(角度) |
描述 |
acos(x) |
acosd(x) |
反余弦 |
asin(x) |
asind(x) |
反正弦 |
atan(x) |
atand(x) |
反正切 |
atan2(y, x) |
atan2d(y, x) |
y/x 的反正切 |
cos(x) |
cosd(x) |
余弦 |
cot(x) |
cotd(x) |
余切 |
sin(x) |
sind(x) |
正弦 |
tan(x) |
tand(x) |
正切 |